package demo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.aerospike.client.AerospikeClient;
import com.aerospike.client.Bin;
import com.aerospike.client.Host;
import com.aerospike.client.IAerospikeClient;
import com.aerospike.client.Key;
import com.aerospike.client.Record;
import com.aerospike.client.ScanCallback;
import com.aerospike.client.Value;
import com.aerospike.client.async.EventLoop;
import com.aerospike.client.async.EventLoops;
import com.aerospike.client.async.EventPolicy;
import com.aerospike.client.async.NioEventLoops;
import com.aerospike.client.cdt.MapOrder;
import com.aerospike.client.cdt.MapPolicy;
import com.aerospike.client.cdt.MapWriteMode;
import com.aerospike.client.cluster.Node;
import com.aerospike.client.policy.ClientPolicy;
import com.aerospike.client.policy.WritePolicy;

// abstract 修饰，避免工具类实例化
public abstract class AsClientUtil {
	
	// 可以配置多个
    // 10.57.30.214
    // 10.57.34.20
    // 10.57.30.38
    // 10.58.16.18
	private static final String SERVER = "10.57.30.38";
	private static final int PORT = 3000;

	public static final String NAMESPACE = "ns1";
	public static final String DATASET = "ashello";
	public static final String BIN = "bin";
	
	private static final MapPolicy mapPolicy = new MapPolicy(MapOrder.KEY_ORDERED, MapWriteMode.UPDATE);
	private static final WritePolicy defaultWritePolicy = new WritePolicy();

	private static volatile int maxConnsPerNode = 10;
	
	private static volatile IAerospikeClient client = null;
	
    static final EventLoops eventLoops = new NioEventLoops(new EventPolicy(),3);

	
	
	static{
		try{
			init();
		}catch(Throwable e){
			throw new RuntimeException("AsClientUtil init error",e);
		}
	}
	
	
	public static final IAerospikeClient getClient(){
		return client;
	}
	
	
	public static Object get(IAerospikeClient client, String namespace, String dataSetName,String key)throws Exception{
		final Key k = new Key(namespace, dataSetName, key);
		final Record record = client.get(null, k);
		if (record == null){
			return null;
		}
		// return record.getValue(bin);
		return record;
	}
	
	public static void put(IAerospikeClient client, String namespace, String dataSetName,String key,String bin,Object value)throws Exception{
		final Key k = new Key(namespace, dataSetName, key);
		Bin binValue = new Bin(bin,Value.get(value));
		client.put(null, k,binValue);	
	}
	
	public static void put(IAerospikeClient client,WritePolicy policy, String namespace, String dataSetName,String key,String bin,Object value)throws Exception{
		final Key k = new Key(namespace, dataSetName, key);
		Bin binValue = new Bin(bin,Value.get(value));
		client.put(policy, k,binValue);	
	}
	
	
	public static boolean delete(IAerospikeClient client, String namespace, String dataSetName,String key,Object value)throws Exception{
		final Key k = new Key(namespace, dataSetName, key);	
		return client.delete(null, k);	
	}
	
	public static EventLoop nextEventLoop() {
	    return eventLoops.next();
	}
	
	private static void init()throws Exception{		
		IAerospikeClient clientTmp;
		ClientPolicy cp = new ClientPolicy();
		// 支持异步 20211020
	    cp.eventLoops = eventLoops;
	    
		cp.maxConnsPerNode = maxConnsPerNode;
		Host host = new Host(SERVER, PORT);		
		clientTmp = new AerospikeClient(cp, host);
		client = clientTmp;	
	}
	
	public static String getServerInfo() {
	    return SERVER+":"+PORT;
	}
	
	
	public static void main(String[] args) throws Exception {
		//System.out.println("hello");
        System.out.println(AsClientUtil.getServerInfo());

		IAerospikeClient client = getClient();
		System.out.println("client="+client);
		
		
		Node[] nodes = client.getNodes();
		System.out.println("node num "+nodes.length);
		// System.out.println();

		for(Node node:nodes){
			System.out.println(node);
		}
		System.out.println();

		
		
		// long double string list map
		List<String> list = new ArrayList<String>();
		list.add("a");
		list.add("b");
		list.add("c");
		
		Map<String,String> map = new HashMap<String,String>();
		map.put("a","1");
		map.put("b","2");
		map.put("c","3");

		
		Object[] values = new Object[]{100L,99.99,"hello",list,map};
		String[] keys = new String[]{"long","double","string","list","map"};
		int num = values.length;
		
		for(int i=0;i<num;i++){
			put(client,NAMESPACE,DATASET,keys[i],BIN,values[i]);	
		}
		
		for(int i=0;i<num;i++){
			System.out.println(get(client,NAMESPACE,DATASET,keys[i]));
		}
		System.out.println();

		//
		System.out.println("long bin add test");
		Key key = new Key(NAMESPACE,DATASET,"long");
		Bin bin = new Bin(BIN,7);
		client.add(null, key, bin);
		System.out.println(client.get(null, key));
		System.out.println();

		
		System.out.println("string bin append test");
		key = new Key(NAMESPACE,DATASET,"string");
		bin = new Bin(BIN," tiger");
		client.append(null, key, bin);
		System.out.println(client.get(null, key));
		System.out.println();

		// scan all
		System.out.println("scan all");
		ScanCallback callback = new DefaultScanCallback();
		client.scanAll(null, NAMESPACE, DATASET, callback, null);
		System.out.println();

		for(Node node:nodes){
			System.out.println("scan node "+node);
			client.scanNode(null, node, NAMESPACE, DATASET, callback, null);
			System.out.println();
		}		
		
		client.close();
	}
	
	
	
	
}
	